<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发送短信倒计时</title>
</head>

<body>
    <input type="text" name="phone" id="phone">
    <input type="button" value="发送" id="send">
    <script>
        var txt = document.querySelector('#phone');
        var btn = document.querySelector('#send');
        var count = 10;  //设置全局变量，表示计时次数
        var time = null;
        btn.onclick = function () {
            //.trim()去掉空格
            if (txt.value.trim() == '') {//判断是否是空字符串
                alert('填写电话');
            } else {
                alert('已发送');
                //禁用发送按钮
                this.disabled = 'disabled';
                //开启计时器
                setInterval(function () {
                    count--;  //次数减一
                    //将时间显示在对应文本中
                    document.querySelector('#send').value = count + 's';
                    // console.log(count);
                    if (count == 0) {
                        //启用按钮
                        btn.enabled = 'enabled';
                        // 还原计时器
                        // count = 60;
                        clearInterval(time);
                        count = 10;
                    }
                }, 1000);

            }
        }
    </script>
</body>

</html>